package com.hexiang.service;

import com.hexiang.DemoService;
import com.hexiang.entity.User;
import com.hexiang.entity.UserRequest;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Service
public class GenericDemoService implements DemoService {
    @Override
    public String sayHello(String name) {
        // 普通编码配置方式
        ApplicationConfig application = new ApplicationConfig();
        application.setName("demo-consumer");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("multicast://224.5.6.7:1234");

        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface("com.imooc.springboot.dubbo.demo.DemoService");
        // 声明为泛化接口
        reference.setGeneric(true);

        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);

        // 基本类型以及Date,List,Map等不需要转换，直接调用
        Object result = genericService.$invoke("sayHello", new String[] { "java.lang.String" },
                new Object[] { name });

        return String.valueOf(result);
    }

    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        return null;
    }

    @Override
    public CompletableFuture<String> sayHelloCompletableFuture(String name) {
        return null;
    }

    @Override
    public User getUser(UserRequest request) {
        return null;
    }

    @Override
    public String timeout(String name) {
        return null;
    }

    @Override
    public CompletableFuture<String> asyncTimeout(String name) {
        return null;
    }

    @Override
    public String err(String name) {
        return null;
    }
}
